package com.os.controller;

import cn.hutool.core.io.IoUtil;
import com.os.util.Hash;
import com.os.util.IoUtilPlus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author zhaozhikai
 * @date 2023/12/23 17:45
 */
@RestController
@RequestMapping("demo")
public class DemoContriller {

    @RequestMapping("/put1")
    public void put(@RequestParam("file") MultipartFile file) throws IOException {
        long start = System.currentTimeMillis();
        InputStream inputStream = file.getInputStream();
        FileOutputStream outputStream = new FileOutputStream(file.getOriginalFilename());
        String hash = IoUtilPlus.copyWithHash(inputStream, outputStream);
        outputStream.close();
        long end = System.currentTimeMillis();
        System.out.println(hash);
        System.out.println("耗时：" + (end - start));
    }

    @RequestMapping("/put2")
    public void put2(@RequestParam("file") MultipartFile file) throws IOException {
        long start = System.currentTimeMillis();
        byte[] bytes = file.getBytes();
        FileOutputStream outputStream = new FileOutputStream(file.getOriginalFilename());
        IoUtil.copy(file.getInputStream(), outputStream);
        String hash = Hash.hash(bytes);
        outputStream.close();
        long end = System.currentTimeMillis();
        System.out.println(hash);
        System.out.println("耗时：" + (end - start));
    }
}
